#define  _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>

using namespace std;

//水果成篮
//第一版，想用数组代替的哈希表来解决这道题，但不对，错在我需要利用哈希表的接口统计和删除，如果像字符那样使用会超
class Solution {
public:
    int count(int* arr, int n)
    {
        int num = 0;
        for (int i = 0; i < n; i++)
        {
            if (arr[i] != 0)
            {
                num++;
            }
        }
        return num;
    }

    int totalFruit(vector<int>& fruits) {
        int hash[128] = { 0 };
        int n = fruits.size();
        int target = 0;
        int ret = 0;
        for (int left = 0, right = 0; right < n; right++)
        {
            hash[fruits[right]]++;
            target = count(hash, 128);
            while (target > 2)
            {
                hash[fruits[left++]]--;
                target = count(hash, 128);
            }
            ret = max(ret, right - left + 1);
        }
        return ret;


    }
};
//第二次根据题意取巧限制了数组的范围，没用使用map的哈希表
class Solution {
public:
    int count(int* arr, int n)
    {
        int num = 0;
        for (int i = 0; i < n; i++)
        {
            if (arr[i] != 0)
            {
                num++;
            }
        }
        return num;
    }

    int totalFruit(vector<int>& fruits) {
        int hash[100001] = { 0 };
        int n = fruits.size();
        int target = 0;
        int ret = 0;
        for (int left = 0, right = 0; right < n; right++)
        {
            hash[fruits[right]]++;
            target = count(hash, 100001);
            while (target > 2)
            {
                hash[fruits[left++]]--;
                target = count(hash, 100001);
            }
            ret = max(ret, right - left + 1);
        }
        return ret;


    }
};

//第三次最终版
class Solution {
public:
    int totalFruit(vector<int>& fruits) {
        int hash[100001] = { 0 };
        int n = fruits.size();
        int target = 0;
        int ret = 0;
        for (int left = 0, right = 0; right < n; right++)
        {
            if (hash[fruits[right]] == 0) target++;
            hash[fruits[right]]++;
            while (target > 2)
            {
                hash[fruits[left]]--;
                if (hash[fruits[left]] == 0) target--;
                left++;
            }
            ret = max(ret, right - left + 1);
        }
        return ret;


    }
};
int main()
{

	return 0;
}